package hit.dao.serialization;

import hit.core.HomeInventoryTracker;
import hit.dao.DAOBase;
import hit.dao.DataAccessObject;
import hit.dao.SettingsDAO;
import hit.persistence.SerializationHandler;

import java.io.File;

/**
 * The main DAO for the Serialization flavor of persistence.
 */
public class SerializationDAO implements DataAccessObject{
	SerializationDAOFactory factory;
	
	/**
	 * Default constructor.
	 */
	public SerializationDAO(){
		factory = new SerializationDAOFactory();
	}
	
	/**
	 * Load the HIT system from the serialization file. 
	 * @return the loaded {@link HomeInventoryTracker}
	 */
	@Override
	public HomeInventoryTracker Load() {
		
		HomeInventoryTracker singletonHIT = SerializationHandler.loadData(
				new File(SerializationHandler.PERSISTENCE_FILE));
		HomeInventoryTracker.LoadHit(singletonHIT);
		
		return HomeInventoryTracker.Instance();
	}

	/**
	 * Save all the data in the HIT system to a serialization file
	 * @param hit the {@link HomeInventoryTracker} to save
	 */
	@Override
	public void Close(HomeInventoryTracker hit) {
		
		SerializationHandler.saveData(hit, 
				new File(SerializationHandler.PERSISTENCE_FILE));
	}

	/**
	 *
	 * @return the DAO responsible for all StorageUnit operations
	 */
	@Override
	public DAOBase GetStorageUnitDAO() {
		return factory.CreateStorageUnitDAO();
	}

	/**
	 *
	 * @return the DAO responsible for all ProductGroup operations
	 */
	@Override
	public DAOBase GetProductGroupDAO() {
		return factory.CreateProductGroupDAO();
	}

	/**
	 *
	 * @return the DAO responsible for all Product operations
	 */
	@Override
	public DAOBase GetProductDAO() {
		return factory.CreateProductDAO();
	}

	/**
	 *
	 * @return the DAO responsible for all Item operations
	 */
	@Override
	public DAOBase GetItemDAO() {
		return factory.CreateItemDAO();
	}

	/**
	 *
	 * @return the DAO responsible for all Item operations
	 */
	@Override
	public SettingsDAO GetSettingsDAO() {
		return factory.CreateSettingsDAO();
	}

	@Override
	public void StartTransaction() {
		return;
	}

	@Override
	public void EndTransaction(boolean commit) {
		return;
	}

	@Override
	public void CloseConnection() { 
		return;
	}
}
